package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.jfinal.core.Controller;

import constant.Msg;
import model.User;
import validate.UserValidate;

public class UserController extends Controller {
	public void index() {
		int userid;
		String id = getPara("id");
		String code = getPara("code");
		String valid = null;
		switch (getRequest().getMethod()) {
		case "GET":
			userid = User.userCache.get(getHeader("token"));
			valid = UserValidate.get(userid,id);
			if (valid != null) {
				renderJson(valid);
				return;
			}
			String result = User.dao.selectById(Integer.parseInt(id));
			if (result == null)
				renderJson(Msg.ERROR_UNKNOWN);
			else
				renderJson(result);
			break;
		case "POST":
			valid = UserValidate.post(code);
			if (valid != null) {
				renderJson(valid);
				return;
			}
			renderJson(User.dao.registerLogin(code));
			break;
		case "PUT":
			userid = User.userCache.get(getHeader("token"));
			valid = UserValidate.put(userid);
			//System.out.println(getAttr("nickName"));
			Map<String, String> paraMap = new HashMap<String, String>();
			try {
				BufferedReader in = new BufferedReader(new InputStreamReader(getRequest().getInputStream()));
				String line;
			    while ((line = in.readLine()) != null){
			    	System.out.println(line);
			    	for (String str : line.split("&")) {
			    		System.out.println(str);
			    		if(str.split("=").length!=2){
			    			continue;
			    		}
						paraMap.put(str.split("=")[0], str.split("=")[1]);
					}
			    	
			    }
			} catch (IOException e) {
				e.printStackTrace();
				renderJson(Msg.ERROR_UNKNOWN);
    			return;
			}
			if (valid != null) {
				renderJson(valid);
				return;
			}
			
			for (Entry<String, String> entry : paraMap.entrySet()) {
				System.out.println(entry.getKey()+" : "+entry.getValue());
			}
			
			
			renderJson(User.dao.update(userid,paraMap.get("nickName"),paraMap.get("avatarUrl"),
					paraMap.get("city"),paraMap.get("province"),paraMap.get("country"),
					paraMap.get("language"),paraMap.get("gender")));
			
			break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
		}
	}

	
	public void sms() {
		String phone = getPara("phone");
		String valid = null;
		switch (getRequest().getMethod()) {
		case "GET":
			valid = UserValidate.sms(phone);
			if (valid != null) {renderJson(valid);return;}
			renderJson(User.dao.sms(phone));
			break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
		}
	}
	
	public void bind() {
		String id = User.userCache.get(getHeader("token"));
		String phone = getPara("phone");
		String validCode = getPara("code");
		String valid = null;
		switch (getRequest().getMethod()) {
		case "GET":
			valid = UserValidate.bind(phone,validCode);
			if (valid != null) {renderJson(valid);return;}
			
			renderJson(User.dao.bind(phone,validCode,Integer.parseInt(id)));
			break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
		}
	}
	
	public void logout(){
		try{
			switch (getRequest().getMethod()) {
			case "GET":
				User.userCache.del(getHeader("token"));
				renderJson(Msg.SUCCESS_EXECUTE);
				break;
			default:
				renderJson(Msg.SUCCESS_EXECUTE);
			}
		}catch(Exception e){
			e.printStackTrace();
			renderJson(Msg.ERROR_UNKNOWN);
		}
	}
	
	/*public void test(){
		User.dao.test();
	}*/
}
